package com.gitee.cirnochat.common.config;

import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;

import java.time.Duration;

@Configuration
public class RedisCacheConfig {

  @Bean
  public RedisCacheConfiguration redisCacheConfiguration(){
      Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
      RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
      configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)).entryTtl(Duration.ofDays(30));
      return configuration;
  }

  @Bean
  public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
      //初始化一个RedisCacheWriter
      RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
      //设置CacheManager的值序列化方式为 fastJsonRedisSerializer,但其实RedisCacheConfiguration默认使用StringRedisSerializer序列化key，
      Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
      RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer);
      RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig()
              .entryTtl(Duration.ofHours(1)) // 设置默认过期时间为1小时
              .serializeValuesWith(pair);
      return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
  }
}

